package com.huawei.hms.update.a;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.update.a.a.a;
import com.huawei.hms.update.a.a.c;
import com.huawei.hms.update.b.b;
import com.huawei.hms.update.provider.UpdateProvider;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.HEX;
import com.huawei.hms.utils.IOUtils;
import com.huawei.hms.utils.SHA256;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public class d implements a {
    private final Context a;
    private final com.huawei.hms.update.b.d b = new b();
    private final a c = new a();
    private com.huawei.hms.update.a.a.b d;
    private File e;

    public d(Context context) {
        this.a = context.getApplicationContext();
    }

    private static boolean a(String str, File file) {
        byte[] digest = SHA256.digest(file);
        if (digest == null || !HEX.encodeHexString(digest, true).equalsIgnoreCase(str)) {
            return false;
        }
        return true;
    }

    private synchronized void a(com.huawei.hms.update.a.a.b bVar) {
        this.d = bVar;
    }

    private synchronized void a(int i, int i2, int i3) {
        if (this.d != null) {
            this.d.a(i, i2, i3, this.e);
        }
    }

    public Context b() {
        return this.a;
    }

    public void a() {
        HMSLog.i("UpdateDownload", "Enter cancel.");
        a(null);
        this.b.b();
    }

    public void a(com.huawei.hms.update.a.a.b bVar, c cVar) {
        Checker.checkNonNull(bVar, "callback must not be null.");
        HMSLog.i("UpdateDownload", "Enter downloadPackage.");
        a(bVar);
        if (cVar == null || !cVar.a()) {
            HMSLog.e("UpdateDownload", "In downloadPackage, Invalid update info.");
            a(2201, 0, 0);
            return;
        }
        if ("mounted".equals(Environment.getExternalStorageState())) {
            Object obj = cVar.b;
            if (TextUtils.isEmpty(obj)) {
                HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package,  packageName is null: ");
                a(2201, 0, 0);
                return;
            }
            this.e = UpdateProvider.getLocalFile(this.a, obj + ".apk");
            if (this.e == null) {
                HMSLog.e("UpdateDownload", "In downloadPackage, Failed to get local file for downloading.");
                a(2204, 0, 0);
                return;
            }
            File parentFile = this.e.getParentFile();
            if (parentFile == null || !(parentFile.mkdirs() || parentFile.isDirectory())) {
                HMSLog.e("UpdateDownload", "In downloadPackage, Failed to create directory for downloading file.");
                a(2201, 0, 0);
                return;
            } else if (parentFile.getUsableSpace() < ((long) (cVar.d * 3))) {
                HMSLog.e("UpdateDownload", "In downloadPackage, No space for downloading file.");
                a(2203, 0, 0);
                return;
            } else {
                try {
                    a(cVar);
                    return;
                } catch (com.huawei.hms.update.b.a e) {
                    HMSLog.w("UpdateDownload", "In downloadPackage, Canceled to download the update file.");
                    a(2101, 0, 0);
                    return;
                }
            }
        }
        HMSLog.e("UpdateDownload", "In downloadPackage, Invalid external storage for downloading file.");
        a(2204, 0, 0);
    }

    void a(c cVar) throws com.huawei.hms.update.b.a {
        HMSLog.i("UpdateDownload", "Enter downloadPackage.");
        OutputStream outputStream = null;
        try {
            String str = cVar.b;
            if (TextUtils.isEmpty(str)) {
                HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package,  packageName is null: ");
                a(2201, 0, 0);
                return;
            }
            this.c.a(b(), str);
            if (!this.c.b(cVar.c, cVar.d, cVar.e)) {
                this.c.a(cVar.c, cVar.d, cVar.e);
                outputStream = a(this.e, cVar.d, str);
            } else if (this.c.b() != this.c.a()) {
                outputStream = a(this.e, cVar.d, str);
                outputStream.a((long) this.c.b());
            } else if (a(cVar.e, this.e)) {
                a(2000, 0, 0);
                this.b.a();
                IOUtils.closeQuietly(null);
                return;
            } else {
                this.c.a(cVar.c, cVar.d, cVar.e);
                outputStream = a(this.e, cVar.d, str);
            }
            int a = this.b.a(cVar.c, outputStream, this.c.b(), this.c.a(), this.a);
            if (a != 200 && a != 206) {
                HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Download the package, HTTP code: " + a);
                a(2201, 0, 0);
                this.b.a();
                IOUtils.closeQuietly(outputStream);
            } else if (a(cVar.e, this.e)) {
                a(2000, 0, 0);
                this.b.a();
                IOUtils.closeQuietly(outputStream);
            } else {
                a(2202, 0, 0);
                this.b.a();
                IOUtils.closeQuietly(outputStream);
            }
        } catch (IOException e) {
            HMSLog.e("UpdateDownload", "In DownloadHelper.downloadPackage, Failed to download." + e.getMessage());
            a(2201, 0, 0);
        } finally {
            this.b.a();
            IOUtils.closeQuietly(outputStream);
        }
    }

    private b a(File file, int i, String str) throws IOException {
        final int i2 = i;
        final String str2 = str;
        return new b(this, file, i) {
            final /* synthetic */ d c;
            private long d = 0;
            private int e = this.c.c.b();

            public void write(byte[] bArr, int i, int i2) throws IOException {
                super.write(bArr, i, i2);
                this.e += i2;
                if (this.e <= 209715200) {
                    long currentTimeMillis = System.currentTimeMillis();
                    if (Math.abs(currentTimeMillis - this.d) > 1000) {
                        this.d = currentTimeMillis;
                        a(this.e);
                    }
                    if (this.e == i2) {
                        a(this.e);
                    }
                }
            }

            private void a(int i) {
                this.c.c.a(this.c.b(), i, str2);
                this.c.a(2100, i, i2);
            }
        };
    }
}
